/**
* 第一周实验 -【实验0  用引用形参做复数四则运算器】
* 学号：201531002134
* 完成时间：2017/09/13
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct complex
{
	float real ;  //实部
	float image ; //虚部
}Complex;


int InitComplex(Complex &C , float vr, float vi);
void OutComplex(Complex C);
Complex AddComplex(Complex &C,Complex C1,Complex C2);
Complex SubComplex(Complex &C,Complex C1,Complex C2);
Complex MulComplex(Complex &C,Complex C1,Complex C2);
Complex SComplex(Complex &C,Complex C1,Complex C2);

//初始化复数，成功返回1
int InitComplex(Complex &C , float vr, float vi)
{
	C.real = vr;
	C.image = vi;
	return 1;
}

void OutComplex(Complex C)
{
	printf("%.2f + %.2fi \n",C.real,C.image);
}

//复数相加
Complex AddComplex(Complex &C,Complex C1,Complex C2)
{
	C.real = C1.real + C2.real ;
	C.image = C1.image + C2.image;
	return C;
}


//复数相减
Complex SubComplex(Complex &C,Complex C1,Complex C2)
{
	C.real = C1.real - C2.real ;
	C.image = C1.image - C2.image;
	return C;
}

//乘
Complex MulComplex(Complex &C,Complex C1,Complex C2)
{
	C.real = ( C1.real * C2.real) - (C1.image*C2.image);
	C.image = (C1.real*C2.image)+(C1.image*C2.real);
	return C;
}

//除
Complex SComplex(Complex &C,Complex C1,Complex C2)
{
	float a=C1.real,b=C1.image,c=C2.real,d=C2.image;
	C.real = (a*c + b*d )/( c*c + d*d );
	C.image = ( b*c - a*d)/(c*c + d*d);
	return C;
}

//菜单
void Menu()
{	
	printf("**************菜单*************\n");
	printf("* 1.输入复数C1                *\n");
	printf("* 2.输入复数C2                *\n");
	printf("* 3.输出复数C1                *\n");
	printf("* 4.输出复数C2                *\n");
	printf("* 5.求C1和C2的和              *\n");
	printf("* 6.求C1和C2的差              *\n");
	printf("* 7.求C1和C2的积              *\n");
	printf("* 8.求C1和C2的商              *\n");
	printf("* 0.结束                      *\n");
	printf("*******************************\n");	
}


int main()
{
	int no;
	float a,b;
	Complex c1,c2,c;
	c1.real = c1.image = c2.real = c2.image = 0;
	Menu();

	while (1)
	{
		printf("*请输入您的选项:");
		scanf("%d",&no);
		switch(no)
		{
		case 0:
			system("pause");
			return 0;
		case 1:
			printf("复数C1 = a+bi 请设置\n");
			printf("实部a=");
			scanf("%f",&a);
			printf("虚部b=");
			scanf("%f",&b);
			InitComplex(c1,a,b);
			break;
		case 2:
			printf("复数C2 = c+di 请设置\n");
			printf("实部c=");
			scanf("%f",&a);
			printf("虚部d=");
			scanf("%f",&b);
			InitComplex(c2,a,b);
			break;
		case 3:
			if ( c1.real == 0 && c1.image == 0)
			{
				printf("-----请先输入复数C1的值!\n");
				break;
			}
			else
			{
				OutComplex(c1);
				break;
			}
		case 4:
			if ( c2.real == 0 && c2.image == 0)
			{
				printf("-----请先输入复数C2的值!\n");
				break;
			}
			else
			{
				OutComplex(c2);
				break;
			}
		case 5:
			if ( c1.real == 0 && c1.image == 0)
			{
				printf("-----请先输入复数C1的值!\n");
				break;
			}
			else if ( c2.real == 0 && c2.image == 0)
			{
				printf("-----请先输入复数C2的值!\n");
				break;
			}
			else
			{
				AddComplex(c,c1,c2);
				OutComplex(c);
				break;
			} 
		case 6:
			if ( c1.real == 0 && c1.image == 0)
			{			
				printf("-----请先输入复数C1的值!\n");
				break;
			}
			else if ( c2.real == 0 && c2.image == 0)
			{
				printf("-----请先输入复数C2的值!\n");
				break;
			}
			else
			{
				SubComplex(c,c1,c2);
				OutComplex(c);
				break;
			} 
		case 7:
			if ( c1.real == 0 && c1.image == 0)
			{
				printf("-----请先输入复数C1的值!\n");
				break;
			}
			else if ( c2.real == 0 && c2.image == 0)
			{
				printf("-----请先输入复数C2的值!\n");
				break;
			}
			else
			{
				MulComplex(c,c1,c2);
				OutComplex(c);
				break;
			} 
		case 8:
			if ( c1.real == 0 && c1.image == 0)
			{
				printf("-----请先输入复数C1的值!\n");
				break;
			}
			else if ( c2.real == 0 && c2.image == 0)
			{
				printf("-----请先输入复数C2的值!\n");
				break;
			}
			else
			{
				SComplex(c,c1,c2);
				OutComplex(c);
				break;
			} 
		default:
			printf("-----ERROR！请输入正确选项！");
		
		}
	}

	
	return 0;
}


